package com.lwb.ztx.videos.model;

import com.lwb.ztx.bean.VideoBean;
import com.lwb.ztx.bean.VideoMainBean;
import com.lwb.ztx.bean.VideoTypeBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by luweibin on 2016/5/30.
 */
public class VideoReadUtils {

    public static VideoMainBean readVideoMainJson(String res) {
        VideoMainBean bean = new VideoMainBean();
        bean.setBeans(readVideoJson(res));

        List<VideoTypeBean> videoTypeBeans = new ArrayList<>();
        try {
            JSONArray resArr = new JSONArray(res);
            if(resArr != null) {
                for (int i=0;i<resArr.length();i++) {
                    JSONObject videos = resArr.optJSONObject(i);
                    JSONArray types = videos.getJSONArray("types");
                    if(types != null) {
                        for (int j=0;j<types.length();j++) {
                            JSONObject type = types.getJSONObject(j);
                            VideoTypeBean videoTypeBean = new VideoTypeBean();
                            videoTypeBean.setId(type.optString("id",""));
                            videoTypeBean.setName(type.optString("name",""));
                            videoTypeBeans.add(videoTypeBean);
                        }
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        bean.setTypes(videoTypeBeans);
        return  bean;
    }

    public static List<VideoBean> readVideoJson(String res) {
        List<VideoBean> beans = new ArrayList<>();
        try {
            JSONArray resArr = new JSONArray(res);
            if(resArr != null) {
                for (int i=0;i<resArr.length();i++) {
                    JSONObject videos = resArr.optJSONObject(i);
                    JSONArray items = videos.getJSONArray("item");
                    for(int j=0;j<items.length();j++) {
                        JSONObject video = items.optJSONObject(j);
                        VideoBean bean = new VideoBean();
                        bean.setCommentsall(video.optString("commentsall",""));
                        bean.setCommentsUrl(video.optString("commentsUrl",""));
                        bean.setDocumentId(video.optString("documentId",""));
                        bean.setDuration(video.optString("duration",""));
                        bean.setImage(video.optString("image",""));
                        bean.setPlayTime(video.optString("playTime",""));
                        bean.setShareUrl(video.optString("shareUrl",""));
                        bean.setTitle(video.optString("title",""));
                        bean.setVideoSize(video.optString("video_size",""));
                        bean.setVideoUrl(video.optString("video_url",""));
                        beans.add(bean);
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return beans;
    }

}
